package com.zcgame.customview.fresh

import android.view.View
import kotlin.math.abs

/**
 *@author:huangLJ
 *@date:2021/5/10 18:22
 */
interface PullHeader:OnPullListener {
    fun createHeaderView(refreshLayout: PullRefreshLayout): View
    fun getConfig():HeaderConfig
    //是否需要覆盖view
    fun needCoverContent():Boolean
    abstract class HeaderConfig{
        //设置刷新偏移量
        abstract fun offsetToRefresh(refreshView: PullRefreshLayout, headerView: View): Int
        //设置刷新后保持的偏移量
        abstract fun offsetToKeepHeaderWhileLoading(refreshView: PullRefreshLayout, headerView: View): Int
        //目标偏移量
        abstract fun totalDistance(refreshView: PullRefreshLayout, headerView: View): Int
        abstract fun headerViewLayoutOffset(refreshView: PullRefreshLayout, headerView: View): Int
        //判断子控件还能不能向上滑动
        abstract fun contentCanScrollUp(refreshView: PullRefreshLayout, contentView: View): Boolean
        //分发滑动的距离
        abstract fun dispatchTouchMove(refreshView: PullRefreshLayout, dy: Int, currentDistance: Int, totalDistance: Int): Int
    }
    class DefaultHeaderConfig : HeaderConfig(){
        override fun offsetToRefresh(refreshView: PullRefreshLayout, headerView: View): Int {
            return (headerView.measuredHeight * 1.2f).toInt()
        }

        override fun offsetToKeepHeaderWhileLoading(refreshView: PullRefreshLayout, headerView: View): Int {
            return headerView.measuredHeight
        }

        override fun totalDistance(refreshView: PullRefreshLayout, headerView: View): Int {
            return headerView.measuredHeight * 3
        }

        override fun headerViewLayoutOffset(refreshView: PullRefreshLayout, headerView: View): Int {
            return headerView.measuredHeight
        }

        override fun contentCanScrollUp(refreshView: PullRefreshLayout, contentView: View): Boolean {
            return contentView.canScrollVertically( -1)
        }

        override fun dispatchTouchMove(refreshView: PullRefreshLayout, dy: Int, currentDistance: Int, totalDistance: Int):Int {
            val hDataY = dy /2
            val ps = (-hDataY * abs(currentDistance)) / totalDistance - hDataY
            return (dy + ps * 0.9f).toInt()
        }
    }
}